package com.poly.oss.controller;

import com.poly.common.core.domain.R;
import com.poly.common.core.tools.utils.Func;
import com.poly.oss.model.FileInfoModel;
import com.poly.oss.service.EndpointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * Minio对象存储端点
 *
 * @author Qingyu Chen
 */
@Api("文件管理")
@RestController
@AllArgsConstructor
@RequestMapping("/endpoint")
public class EndpointController {

    /**
     * 对象存储构建类
     */
    private final EndpointService endpointService;

    /**
     * 获取文件信息
     *
     * @param fileName 存储桶对象名称
     * @return InputStream
     */
    @SneakyThrows
    @GetMapping("/stat-file")
    public R<FileInfoModel> statFile(@RequestParam String fileName) {
        return R.ok(endpointService.statFile(fileName));
    }

    /**
     * 上传文件
     *
     * @param file 文件
     * @return ObjectStat
     */
    @ApiOperation("上传文件")
    @SneakyThrows
    @PostMapping("/put-file")
    public R<FileInfoModel> putFile(@RequestParam MultipartFile file) {
        return R.ok(endpointService.upload(file));
    }


    /**
     * 删除文件
     *
     * @param fileName 存储桶对象名称
     * @return R
     */
    @SneakyThrows
    @PostMapping("/remove-file")
    public R removeFile(@RequestParam String fileName) {
        endpointService.remove(fileName);
        return R.ok("操作成功");
    }

    /**
     * 批量删除文件
     *
     * @param fileNames 存储桶对象名称集合
     * @return R
     */
    @SneakyThrows
    @PostMapping("/remove-files")
    public R removeFiles(@RequestParam String fileNames) {
        endpointService.removeBatch(Func.toStrList(fileNames));
        return R.ok("操作成功");
    }
}
